package com.ems.servlet;

import com.ems.dao.DepartmentDAO;
import com.ems.model.Department;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            DepartmentDAO departmentDAO = new DepartmentDAO();
            List<Department> departments = departmentDAO.getAllDepartments();
            request.setAttribute("departments", departments);

            RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/department.jsp");
            dispatcher.forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "获取部门列表失败，请稍后再试");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/department.jsp");
            dispatcher.forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        DepartmentDAO departmentDAO = new DepartmentDAO();

        try {
            if ("add".equals(action)) {
                String deptName = request.getParameter("deptName");
                Department dept = new Department();
                dept.setDeptName(deptName);

                if (departmentDAO.addDepartment(dept)) {
                    response.sendRedirect("department");
                } else {
                    request.setAttribute("error", "添加部门失败，请稍后再试");
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/department.jsp");
                    dispatcher.forward(request, response);
                }
            } else if ("delete".equals(action)) {
                int deptId = Integer.parseInt(request.getParameter("deptId"));

                if (departmentDAO.deleteDepartment(deptId)) {
                    response.sendRedirect("department");
                } else {
                    request.setAttribute("error", "删除部门失败，请稍后再试");
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/department.jsp");
                    dispatcher.forward(request, response);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "操作失败，请稍后再试");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/admin/department.jsp");
            dispatcher.forward(request, response);
        }
    }
}